package com.health.ts.controller.consultation;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.consultation.AssignInfo;
import com.health.ts.entity.consultation.ConsultationForm;
import com.health.ts.entity.consultation.ConsultationInfo;
import com.health.ts.entity.consultation.RequestParam;
import com.health.ts.entity.consultation.ResponseParam;
import com.health.ts.entity.consultation.VQueryConsulation;
import com.health.ts.service.consultation.AssignInfoService;
import com.health.ts.service.consultation.ConsultationInfoService;
import com.health.ts.service.consultation.ConsultationListEnum;
import com.health.ts.service.consultation.ConsultationLogService;
import com.health.ts.service.consultation.VQueryConsulationService;
import com.health.ts.utils.SessionUserUtil;
/**
 * 会诊
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/api/consultation",produces= BaseController.JSON_UTF8)
public class ConsultationController extends BaseController{
	
	@Autowired
	ConsultationInfoService consultationInfoService;
	
	@Autowired
	ConsultationLogService consultationLogService;
	
	@Autowired
	VQueryConsulationService  vqueryConsulationService;
	
	@Autowired
	AssignInfoService assignInfoService;
	
	/**  
	* @Title: saveConsultation  
	* @Description: 保存申请信息,进入待申请状态，返回唯一的会诊id 
	* @param @param form
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult saveConsultation(@RequestBody ConsultationForm form) {
		ResponseParam res = consultationInfoService.saveConsultation(form);
		return ApiResult.success(res);
	}
	
	/**  
	* @Title: applyConsultation  
	* @Description: 提交会诊申请,待前质控(无暂存，直接提交)
	* @param @param isCon  是否已保存
	* @param @param con_class 
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/apply", method=RequestMethod.POST)
	public ApiResult applyConsultation(@RequestBody ConsultationForm form) {
		ResponseParam res = consultationInfoService.applyConsultation(form);
		return ApiResult.success(res);
	}
	
	/**  
	* @Title: qualityControlDone  
	* @Description:前质控通过
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/qcBefore/{conid}/done", method=RequestMethod.PUT)
	public ApiResult qualityControlDone(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		String remark=info.getRemark();
		consultationInfoService.qualityControlBefore(conid,remark);
		return ApiResult.success();
	}
	
	/**  
	* @Title: qualityControlBack  
	* @Description: 前质控不通过，待申请
	* @param conid 会诊id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 

	@RequestMapping(value="/qcBefore/{conid}/back", method=RequestMethod.PUT)
	public ApiResult qualityControlBack(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.qualityControlBeforeRock(conid,info);
		return ApiResult.success();
	}
	
	/**  
	* @Title: qualityControlDone2  
	* @Description: 二级前质控审核通过，待分诊 
	* @param @param consultationId
	* @param @param con_class
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/qcBefore2/{conid}/done", method=RequestMethod.PUT)
	public ApiResult qualityControlDone2(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.qualityControlBefore2(conid);
		return ApiResult.success();
	}
	
	/**  
	* @Title: qualityControlBack2  
	* @Description: 二级前质控不通过，待前质控
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/qcBefore2/{conid}/back", method=RequestMethod.PUT)
	public ApiResult qualityControlBack2(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.qualityControlBeforeRock2(conid,info);
		return ApiResult.success();
	}
	
	/**  
	* @Title: assignDone  
	* @Description: 分诊通过，待报告  
	* @param @param type
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/assign/{conid}/done", method=RequestMethod.PUT)
	public ApiResult assignDone(@PathVariable String conid,@RequestBody RequestParam reqParam) {
		consultationInfoService.assignDone(conid,reqParam);
		return ApiResult.success();
	}
	
	/**  
	* @Title: assignBack  
	* @Description: 分诊不通过，待二级前质控
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/assign/{conid}/back", method=RequestMethod.PUT)
	public ApiResult assignBack(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.assignBack(conid,info);
		return ApiResult.success();
	}
	

	/**  
	* @Title: writeReport  
	* @Description: 写报告，结束或者待后质控 
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/report/{conid}/done", method=RequestMethod.PUT)
	public ApiResult writeReport(@PathVariable String conid,@RequestBody RequestParam reqParam) {
		consultationInfoService.writeReport(conid,reqParam);
		return ApiResult.success();
	}
	
	/**  
	* @Title: writeReportBack  
	* @Description: 报告退回，待分诊
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/report/{conid}/back", method=RequestMethod.PUT)
	public ApiResult writeReportBack(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.writeReportBack(conid,info);
		return ApiResult.success();
	}
	
	/**  
	* @Title: qualityControlAfterDone  
	* @Description:后质控通过，流程结束
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/qcAfter/{conid}/done", method=RequestMethod.PUT)
	public ApiResult qualityControlAfterDone(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		String remark=info.getRemark();
		consultationInfoService.qualityControlAfter(conid,remark);
		return ApiResult.success();
	}
	
	/**  
	* @Title: qualityControlAfterBack  
	* @Description: 后质控不通过，待报告
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/qcAfter/{conid}/back", method=RequestMethod.PUT)
	public ApiResult qualityControlAfterBack(@PathVariable String conid,@RequestBody ConsultationInfo info) {
		consultationInfoService.qualityControlAfterRock(conid,info);
		return ApiResult.success();
	}
	
	/**  
	* @Title: cancelApply  
	* @Description: 取消会诊申请
	* @param @param reqParam
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/cancelApply/{conid}", method=RequestMethod.PUT)
	public ApiResult cancelApply(@PathVariable String conid) {
		consultationInfoService.cancelApply(conid);
		return ApiResult.success();
	}
	
	/**
	 * 根据条件查询会诊待办列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/list/{listType}/unfinish", method=RequestMethod.GET)
	public ApiResult queryConsultatiosnUnfinish(@PathVariable String listType,FrontPage page){
		QueryWrapper<VQueryConsulation> wrapper = new QueryWrapper<VQueryConsulation>();
		//患者名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("apply_pname", patient_name); 
		}
		//会诊类别
		String con_class =getParam(page,"con_class");
		if (StringUtils.isNotEmpty(con_class)){
			wrapper.eq("con_class", con_class); 
		}
		//时间区间
		String start_date =getParam(page,"start_date");
		String end_date =getParam(page,"end_date");
		if (StringUtils.isNotEmpty(start_date)){
			wrapper.between("apply_time", start_date, end_date); 
		}
		
		if(ConsultationListEnum.QC_BEFORE.toString().equals(listType)){
		//列表类型为前质控
			wrapper.in("con_status", ConsultationListEnum.QC_BEFORE.getUnfinishStatus());
			//新增申请医院模糊查询
			String apply_hname =getParam(page,"apply_hname");
			wrapper.like("apply_hname", apply_hname);
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.ASSIGN.toString().equals(listType)){
		//列表类型为分诊
			wrapper.in("con_status", ConsultationListEnum.ASSIGN.getUnfinishStatus());
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.MY_TASK.toString().equals(listType)){
		//列表类型为我的任务
			wrapper.in("con_status", ConsultationListEnum.MY_TASK.getUnfinishStatus());
			//用户在分诊列表中
			wrapper.in("id", assignConList());
		}else if(ConsultationListEnum.QC_AFTER.toString().equals(listType)){
		//列表类型为后质控
			wrapper.in("con_status", ConsultationListEnum.QC_AFTER.getUnfinishStatus());
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.MY_APPLY.toString().equals(listType)){
		//列表类型为我的申请
			wrapper.in("con_status", ConsultationListEnum.MY_APPLY.getUnfinishStatus());
			//申请医院为用户所属医院
			wrapper.eq("apply_hid", SessionUserUtil.getUserHosId()); 
			//申请人为用户
			wrapper.eq("apply_uid", SessionUserUtil.getUserId()); 
		}
		//查询可用记录
		wrapper.eq("valid_status", "1"); 

		Page<VQueryConsulation> pageList = vqueryConsulationService.page(page.getPagePlus(), wrapper);
		CustomPage<VQueryConsulation> customPage = new CustomPage<VQueryConsulation>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 根据条件查询会诊已办列表
	 * @param
	 * @return
	 */
	@RequestMapping(value="/list/{listType}/finish", method=RequestMethod.GET)
	public ApiResult queryConsultationsFinish(@PathVariable String listType,FrontPage page){
		QueryWrapper<VQueryConsulation> wrapper = new QueryWrapper<VQueryConsulation>();
		//患者名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("apply_pname", patient_name); 
		}
		//会诊类型
		String con_class =getParam(page,"con_class");
		if (StringUtils.isNotEmpty(con_class)){
			wrapper.eq("con_class", con_class); 
		}
		//时间区间
		String start_date =getParam(page,"start_date");
		String end_date =getParam(page,"end_date");
		if (StringUtils.isNotEmpty(start_date)){
			wrapper.between("apply_time", start_date, end_date); 
		}
		
		if(ConsultationListEnum.QC_BEFORE.toString().equals(listType)){
		//列表类型为前质控
			wrapper.in("con_status", ConsultationListEnum.QC_BEFORE.getFinishStatus());
			//新增申请医院模糊查询
			String apply_hname =getParam(page,"apply_hname");
			wrapper.like("apply_hname", apply_hname);
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.ASSIGN.toString().equals(listType)){
		//列表类型为分诊
			wrapper.in("con_status", ConsultationListEnum.ASSIGN.getFinishStatus());
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.MY_TASK.toString().equals(listType)){
		//列表类型为我的任务
			wrapper.in("con_status", ConsultationListEnum.MY_TASK.getFinishStatus());
			//用户在分诊列表中
			wrapper.in("id", assignConList());
		}else if(ConsultationListEnum.QC_AFTER.toString().equals(listType)){
		//列表类型为后质控
			wrapper.in("con_status", ConsultationListEnum.QC_AFTER.getFinishStatus());
			//指定医院为用户所属医院
			wrapper.eq("assign_hid", SessionUserUtil.getUserHosId()); 
		}else if(ConsultationListEnum.MY_APPLY.toString().equals(listType)){
		//列表类型为我的申请
			wrapper.in("con_status", ConsultationListEnum.MY_APPLY.getFinishStatus());
			//申请医院为用户所属医院
			wrapper.eq("apply_hid", SessionUserUtil.getUserHosId()); 
			//申请人为用户
			wrapper.eq("apply_uid", SessionUserUtil.getUserId()); 
		}
		//查询可用记录
		wrapper.eq("valid_status", "1"); 
		
		Page<VQueryConsulation> pageList = vqueryConsulationService.page(page.getPagePlus(), wrapper);
		CustomPage<VQueryConsulation> customPage = new CustomPage<VQueryConsulation>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 根据会诊id查询会诊信息
	 * @param conid
	 * @return
	 */
	@RequestMapping(value="/info/{conid}", method=RequestMethod.GET)
	public ApiResult queryInfo(@PathVariable String conid){
		return ApiResult.success(consultationInfoService.queryInfo(conid));
	}
	
	/**
	 * 我的任务查询条件
	 * @return 返回符合分诊时指定医生为用户的会诊id列表
	 */
	private List<String> assignConList(){
		List<String> conIds=new ArrayList<String>();
		QueryWrapper<AssignInfo> assignWrapper = new QueryWrapper<AssignInfo>();
		//分诊时指定医院为用户所在医院
		assignWrapper.eq("allot_hid", SessionUserUtil.getUserHosId());
		//分诊时指定医生为用户id
		assignWrapper.eq("allot_docid", SessionUserUtil.getUserId());
		//会诊未过期
//		assignWrapper.gt("con_start", new Date());
		
		List<AssignInfo> li = assignInfoService.list(assignWrapper);
		if(li.size()>0){
			for(AssignInfo assign:li){
				conIds.add(assign.getCon_id());
			}
		}else{
			//使条件强制生效
			conIds.add("noneId");
		}
		return conIds;
	}
}
